package com.jit.app.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.jit.app.AppMarketApplication;
import com.jit.app.R;
import com.jit.app.listener.DownloadClickListener;
import com.jit.app.listener.InstallClickListener;
import com.jit.app.listener.OpenClickListener;
import com.jit.app.model.Constants;
import com.jit.app.repository.DownloadInfoRepository;
import com.jit.app.util.InstalledApplicationUtils;

import java.util.List;

public class AppListAdapter extends BaseAdapter {

    public AppListAdapter(){}

    Context context;
    List<JSONObject> data;
    int itemLayoutId;

    DownloadInfoRepository downloadInfoRepository;


    public AppListAdapter(Context context, List<JSONObject> data, int itemLayoutId){
        this.context =context;
        this.data = data;
        this.itemLayoutId = itemLayoutId;

        downloadInfoRepository = new DownloadInfoRepository(context);

    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return data.get(position).getLong("id");
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(itemLayoutId,null);
            viewHolder = new ViewHolder();
            viewHolder.picImageView = convertView.findViewById(R.id.iv_pic);
            viewHolder.softwareNameTextView = convertView.findViewById(R.id.tv_softwareName);
            viewHolder.downloadsTextView = convertView.findViewById(R.id.tv_downloads);
            viewHolder.softwareSizeTextView = convertView.findViewById(R.id.tv_softwaresize);
            viewHolder.downloadButton = convertView.findViewById(R.id.btn_download);
            viewHolder.downloadButton.setOnClickListener(new DownloadClickListener());
            viewHolder.downloadProcess = convertView.findViewById(R.id.pb_download);
            viewHolder.installButton = convertView.findViewById(R.id.btn_install);
            viewHolder.installButton.setOnClickListener(new InstallClickListener(context));
            viewHolder.openButton = convertView.findViewById(R.id.btn_open);
            viewHolder.openButton.setOnClickListener(new OpenClickListener(context));
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //更新子项数据
        JSONObject jsonObject = data.get(position);
        //添加应用图片
        Glide.with(context).load(Constants.HOST.FILE_HOST+jsonObject.getString("logoPicPath")).into(viewHolder.picImageView);
        viewHolder.softwareNameTextView.setText(jsonObject.getString("softwareName"));
        viewHolder.downloadsTextView.setText(jsonObject.getInteger("downloads")+"人下载");
        viewHolder.softwareSizeTextView.setText(jsonObject.getFloat("softwareSize")+"MB");
        viewHolder.downloadButton.setTag(jsonObject);
        String pack = jsonObject.getString("apkName");
        viewHolder.downloadButton.setVisibility(View.INVISIBLE);
        viewHolder.downloadProcess.setVisibility(View.INVISIBLE);
        viewHolder.installButton.setVisibility(View.INVISIBLE);
        viewHolder.openButton.setVisibility(View.INVISIBLE);
        if(InstalledApplicationUtils.isInstalled(pack, context)){
            viewHolder.openButton.setVisibility(View.VISIBLE);
            viewHolder.openButton.setTag(pack);
        }else {
            String apkPath = downloadInfoRepository.getDownloadInfoByPack(pack);
            if( apkPath != null){
                viewHolder.installButton.setVisibility(View.VISIBLE);
                Bundle bundle2 = new Bundle();
                bundle2.putString("pack",pack);
                bundle2.putString("apkPath",apkPath);
                viewHolder.installButton.setTag(bundle2);
            }else {
                Double process = AppMarketApplication.downloadBinder.getDownloadProcess(pack);
                if(process != null){
                    viewHolder.downloadProcess.setVisibility(View.VISIBLE);
                    viewHolder.downloadProcess.setProgress((int)(process*100));
                }else{
                    viewHolder.downloadButton.setVisibility(View.VISIBLE);
                }
            }
        }

        return convertView;
    }

    public static class ViewHolder{
        public ImageView picImageView;
        public TextView softwareNameTextView;
        public TextView downloadsTextView;
        public TextView softwareSizeTextView;
        public TextView downloadButton;
        public ProgressBar downloadProcess;
        public TextView installButton;
        public TextView openButton;
    }
}
